﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Crank.Core
{
    public class LinkReorientModelTransform : IModelTransformer
    {
        #region IModelTransformer Members

        public void ApplyTransform(DataModel target)
        {
            //var links = target.Links.ToArray<LinkDef>();
            foreach (var link in target.Links)
            {
                switch (link.Type)
                {
                    case "=>": // Good do nothing
                        break;
                    case "<=": // Just reverse the lhs and rhs
                        Swap(link);
                        break;
                    case ">": // Good do nothing
                        break;
                    case "<": // Just reverse the lhs and rhs
                        Swap(link);
                        break;
                    default:
                        throw new Exception(String.Format("Unknown Link Type in Re-Orientation Transform '{0}'", link.Type));
                }
                link.Type = "=>";
                if (link.Detail.Name == "_")
                    link.Detail.Name = String.Empty;
            }
        }

        #endregion

        private void Swap(LinkDef link)
        {
            EntityDef temp = link.LHS;
            link.LHS = link.RHS;
            link.RHS = temp;
        }
    }
}
